package battleship;

import javax.swing.JOptionPane;
import org.jatha.Jatha;
import org.jatha.dynatype.*;

/**
 *
 * @author Code and Pseudocode: Mike Colmer, Katelyn Doran, Darren Harton, Shaun Pickford
 */
public class Conversion {

    /* This converts the coordinate as a string ("A1, B3, etc") into a number
     * between 1-100...NOT the array position!
     */
   public int toNumber(String coordinate)
    {
       if(coordinate.equals(""))
       {
           return -1;
       }
        /* Declare variables */
    String letter, number;
    int numericalCoordinate = 0;

    /* Seperate letter and number from coordinate */
    letter = coordinate.substring(0,1); //check to see if letter is a-j
    number = coordinate.substring(1); //check to see if number is 1-10
    int tempNum;
    try
    {
        tempNum = Integer.parseInt(number);
    }
    catch(NumberFormatException e)
    {
        JOptionPane.showMessageDialog(null, "The coordinate format is incorrect. Correct format is A-J and 1-10 (ex: A5 or C9)");
        return -1;
    }

    //catch error if number is over 10...added by Mike
    if(tempNum > 10 || tempNum < 1)
    {
        JOptionPane.showMessageDialog(null, "The coordinate format is incorrect. Correct format is A-J and 1-10 (ex: A5 or C9)");
        return -1;
    }

    /* If statements to convert the letter coordinate to a numerical row */
    if (letter.equalsIgnoreCase("a"))
    {
        numericalCoordinate = 0;
    }

    else if (letter.equalsIgnoreCase("b"))
    {
        numericalCoordinate = 10;
    }

    else if (letter.equalsIgnoreCase("c"))
    {
        numericalCoordinate = 20;
    }

    else if (letter.equalsIgnoreCase("d"))
    {
        numericalCoordinate = 30;
    }

    else if (letter.equalsIgnoreCase("e"))
    {
        numericalCoordinate = 40;
    }

    else if (letter.equalsIgnoreCase("f"))
    {
        numericalCoordinate = 50;
    }

    else if (letter.equalsIgnoreCase("g"))
    {
        numericalCoordinate = 60;
    }

    else if (letter.equalsIgnoreCase("h"))
    {
        numericalCoordinate = 70;
    }

    else if (letter.equalsIgnoreCase("i"))
    {
        numericalCoordinate = 80;
    }

    else if (letter.equalsIgnoreCase("j"))
    {
        numericalCoordinate = 90;
    }

    /* Else statement added by Mike to correct user to input if letter is
     * not between A-J */
     else
    {
        JOptionPane.showMessageDialog(null, "The coordinate format is incorrect. Correct format is A-J and 1-10 (ex: A5 or C9)");
        return -1;
    }


    /* Add the number entered by the user to the amount calculated
     * by the letter to number conversion */
    numericalCoordinate = numericalCoordinate + tempNum;
    return numericalCoordinate;

   }

    public String toCoordinate(int number)
    {
        String letter;
        String coordinate = "";
        
        if(number > 0 && number <= 10)
        {
            letter = "A";
            coordinate = letter + number;

        }

        else if(number > 10 && number <= 20)
        {
            letter = "B";
            coordinate = letter + (number - 10);
        }

        else if(number > 20 && number <= 30)
        {
            letter = "C";
            coordinate = letter + (number -20);
        }

        else if(number > 30 && number <= 40)
        {
            letter = "D";
            coordinate = letter + (number - 30);
        }

        else if(number > 40 && number <= 50)
        {
            letter = "E";
            coordinate = letter + (number - 40);
        }

        else if(number > 50 && number <= 60)
        {
            letter = "F";
            coordinate = letter + (number - 50);
        }

        else if(number > 60 && number <= 70)
        {
            letter = "G";
            coordinate = letter + (number - 60);
        }

        else if(number > 70 && number <= 80)
        {
            letter = "H";
            coordinate = letter + (number - 70);
        }

        else if(number > 80 && number <= 90)
        {
            letter = "I";
            coordinate = letter + (number - 80);
        }

        else if(number > 90 && number <= 100)
        {
            letter = "J";
            coordinate = letter + (number - 90);
        }
        else
        {
            coordinate = "error";
        }

        return coordinate;

    }


    public String toCoordinateInLISP(int number)
    {
        String strResult = "";
        /* Create LISP object */
        Jatha myLisp  = new Jatha(false, false);
        //String result;
        myLisp.init();
        myLisp.start();

        String toNumber = "(defun convert (char int) (+ (setq varx (case char(a 0)(b 1)(c 2)(d 3)(e 4)(f 5)(g 6)(h 7)(i 8)(j 9)))(setq vary (* (- int 1) 10))))";
        try
        {
            LispValue result = myLisp.eval(toNumber);
            strResult = result.toString();
        }
        catch (Exception e)
        {
            System.err.println("LISP Exception: " + e);
        }

        return strResult;



    }

    public String toNumberInLISP(String input)
    {
        String resultAsNumber = "";
        /* Create LISP object */
        Jatha myLisp  = new Jatha(false, false);
        //String result;
        myLisp.init();
        myLisp.start();

        String toCoordinate = "(defun convert (num)(setq x (write-to-string num))(when (< (length x) 2)(setq x (concatenate 'string '0' x)))(setq int (write-to-string (+ (read-from-string (string (char x 0))) 1 )))(setq cha (case (parse-integer (string (char x 1)))(0 'a)(1 'b)(2 'c)(3 'd)(4 'e)(5 'f)(6 'g)(7 'h)(8 'i)(9 'j)))(return-from convert (format nil '~A ~B' cha int))) ";

        try
        {
            LispValue result = myLisp.eval(toCoordinate);
            resultAsNumber = result.toString();
        }
        catch (Exception e)
        {
            System.err.println("LISP Exception: " + e);
        }

        return resultAsNumber;

    }
}
